package 设计模式.原型模式;

import java.util.ArrayList;

/**
 * @description:
 * @author: asd
 * @time: 2022/11/23 20:15
 */
public class thing3 implements Cloneable {
    private ArrayList<String> list = new ArrayList<String>();

    @Override
    public thing3 clone() {
        thing3 thing = null;
        try {
            thing = (thing3) super.clone();
            thing.list = (ArrayList) this.list.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            System.out.println("克隆失败");
        }
        return thing;
    }

    public void setValue(String value) {
        this.list.add(value);
    }

    public ArrayList getValue() {
        return this.list;
    }
}

class DeepClone {
    public static void main(String[] args) {
        thing3 thing = new thing3();
        thing.setValue("张三");
        thing3 cloneThing= thing.clone();
        cloneThing.setValue("李四");
        System.out.println("原始对象："+thing.getValue());
        System.out.println("克隆对象："+cloneThing.getValue());
    }
    /*
     * @description: 此为深拷贝，需要注意的是对于final类型的变量是不能进行深拷贝的
     */
}
